<template>
	<view class="main">
		<view class="keyu_logo">
			<image class="logo" :src="imgPrefix('/static/image/icon/keyu_logo.png')"></image>
			<text class="app_name">可语</text>
		</view>
		<view class="btn p-r" @tap.stop="bindWeChatLogin">微信登录
			<button class="share-btn" open-type="getUserInfo" @getuserinfo="getUserInfo"></button>
		</view>
		<view @tap="pageTo('./login')" class="phone_login">手机号登录/注册</view>
	</view>
</template>

<script>
import API from '@/static/api/index.js';
import { commonMixinFun } from '@/static/mixins/common.js';
import request from '@/static/uitls/request.js';
const app = getApp();
export default {
	mixins: [commonMixinFun],
	data() {
		return {};
	},
	methods:{
		  // 更新用户信息
		  getUserInfo(e) {
		    let that = this
		    let {
		      userInfo
		    } = e.detail
				app._log(userInfo)
		    if (userInfo) {
				API.getMineInfo().then(res => {
					if (res.code == 1&&(res.data.user.avatar==''||res.data.user.mobile==''||res.data.user.nickname=='')) {
						API.updateUserInfo({
						  user_id: res.data.user.user_id,
						  avatar: userInfo.avatarUrl,
						  nickname:userInfo.nickName
						}).then(res => {
						  // that._getUserInfo()
						})
					}
				});
		    }
		  },
		  bindWeChatLogin(){
			  let _this = this;
			  uni.login({
			    success: function (res) {
			      request({
			        url: `${app.globalData.BASE_URL}/api/v1/user/auth?code=${res.code}`,
			      }).then(res => {
					  app._log(res)
					  if(res.code==9997){
						  uni.setStorageSync('wxapp_openid',res.data.wxapp_openid);
						  uni.showModal({
						  	title:'温馨提示',
							content:'新用户首次需手机号登录，是否前往？',
							cancelText:'返回',
							confirmText:'登录',
							success: (res) => {
								if(res.confirm){
									_this.pageTo('./login')
								}
							}
						  })
					  }else if (res.code == 1) {
						  app._log(9546)
						_this.showToast('登录成功,正在跳转...');
						app.globalData.LOGION = true;
						uni.setStorageSync('app_token', res.data.token);
						setTimeout(() => {
							_this.pageTo('/pages/index/index');
						}, 1500);
					}else{
						_this.showToast('请稍后再试');
					}
			      })
			    }
			  })
		  }
	}
};
</script>

<style lang="scss">
.main {
	margin: 192rpx auto 0;
	text-align: center;
}
.keyu_logo {
	margin-bottom: 226rpx;
	.logo{
		width: 234rpx;
		height: 234rpx;
		background-color: #ffffff;
		border-radius: 24rpx;
	}
	.app_name {
		display: block;
		margin-top: 16rpx;
		font-size: 34rpx;
		color: #000000;
	}
}
.btn {
	margin:0 48rpx;
	width: 654rpx;
	line-height: 92rpx;
	color: #fff;
	background-image: linear-gradient(270deg, #7f4aff 0%, #ab7bff 100%);
	border-radius: 16rpx;
	text-align: center;
}
.phone_login {
	font-size: 28rpx;
	color: #5e729a;
	text-align: center;
	padding: 24rpx 0;
}
</style>
